package com.hanter.android.radui.mvp;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hanter.android.radui.R;

public abstract class MvpFragment<V extends BaseView, T extends BasePresenter<V>>
        extends BaseFragment implements BaseView, View.OnClickListener {

    protected T presenter;
    private ViewGroup contentContainer;
    private View contentView;
    private ContentLoadStatusViewHelper contentStatusViewHelper;
    private LoadingDialogHelper loadingDialogHelper;
    private boolean showLoading;

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = createPresenter();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentContainer = (ViewGroup) inflater.inflate(R.layout.fragment_rad_mvp, container, false);
        // 初始化
        initBaseContentView();
        return contentContainer;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //noinspection unchecked
        presenter.attachView((V) this);
        loadingDialogHelper = new LoadingDialogHelper(getChildFragmentManager());
        initViews();
    }

    @Override
    public void onDestroyView() {
        if (presenter != null)
            presenter.detachView();
        disProgressDialog();
        super.onDestroyView();
    }

    /**
     * 初始化视图
     */
    private void initBaseContentView() {
        createAddContentView();
        contentStatusViewHelper = new ContentLoadStatusViewHelper(MvpFragment.this, contentContainer, contentView);
        contentStatusViewHelper.initViews(showLoading);
    }

    protected void createAddContentView() {
        contentView = LayoutInflater.from(getContext()).inflate(getLayout(), contentContainer, false);

        if (contentView != null) {
            contentContainer.addView(contentView);
            if (showLoading)
                contentView.setVisibility(View.INVISIBLE);
        }
    }

    protected abstract @LayoutRes int getLayout();

    protected void initViews() {
    }

    @Override
    public Context getContext() {
        return getActivity();
    }

    protected abstract T createPresenter();

    /**
     * 必须在onCreate之前调用
     *
     * @param showLoading 是否显示内容加载中
     */
    public void setShowContentLoading(boolean showLoading) {
        showLoading = showLoading;
    }

    @Override
    public void showProgressDialog() {
        loadingDialogHelper.showProgressDialog();
    }

    @Override
    public void disProgressDialog() {
        loadingDialogHelper.dismissProgressDialog();
    }

    @Override
    public void hideContentView() {
        contentStatusViewHelper.hideContentView();
    }

    @Override
    public void showContentView() {
        contentStatusViewHelper.showContentView();
    }

    @Override
    public void showNoData() {
        contentStatusViewHelper.showNoDataView();
    }

    @Override
    public void hideNoData() {
        contentStatusViewHelper.hideExceptionView();
    }

    @Override
    public void showPageFault() {
        contentStatusViewHelper.showPageFaultView();
    }

    @Override
    public void showNoNetWork() {
        contentStatusViewHelper.showNoNetworkView();
    }

    @Override
    public void showLoadingView() {
        contentStatusViewHelper.showLoadingView();
    }

    @Override
    public void completeLoading() {
        contentStatusViewHelper.completeLoading();
    }

    @Override
    public void navBack() {
        requireActivity().onBackPressed();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btnContentExceptionRefresh) {
            showLoadingView();
            onContentRefreshClick();
        }
    }

    protected void onContentRefreshClick() {

    }

}
